package com.zja.servlet;


import com.zja.dao.Impl.KnowledgeBaseDaoImpl;
import com.zja.dao.KnowledgeBaseDao;
import com.zja.model.JsonResponse;
import com.zja.model.KnowledgeBase;
import com.zja.model.User;
import com.zja.util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
/**
 * @author JA
 * @date: 2025/5/6
 * description:获取我管理的知识库列表
 */
@WebServlet("/api/home/managed-kbs")
public class MyManagedKnowledgeBaseServlet extends HttpServlet {
    private final KnowledgeBaseDao knowledgeBaseDao = new KnowledgeBaseDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");

        // 获取当前登录用户
        User currentUser = (User) req.getSession().getAttribute("currentUser");
        if (currentUser == null) {
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("请先登录"));
            return;
        }

        // 查询该用户是管理员的所有知识库
        List<KnowledgeBase> kbList = knowledgeBaseDao.selectAllByAdminUserId(currentUser.getId());

        // 返回响应
        ServletUtil.writeJsonResponse(resp, JsonResponse.success("获取成功", kbList));
    }
}
